<template>
    <transition name="fade">
      <div 
        v-if="visible"
        class="mask-layer"
        :class="{'clickable': clickToClose}"
        @click="handleClick"
      >
        <div class="mask-content" @click.stop>
          <slot></slot>
          <button v-if="showClose" class="close-btn" @click="close">×</button>
        </div>
      </div>
    </transition>
  </template>
  
  <script>
  export default {
    name: 'MaskLayer',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      clickToClose: {
        type: Boolean,
        default: true
      },
      showClose: {
        type: Boolean,
        default: true
      }
    },
    methods: {
      close() {
        this.$emit('update:visible', false)
        this.$emit('close')
      },
      handleClick() {
        if (this.clickToClose) {
          this.close()
        }
      }
    }
  }
  </script>
  
  <style scoped>
  /* 遮罩层基础样式 */
  .mask-layer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: auto;
  }
  
  /* 可点击区域 */
  .mask-layer.clickable {
    cursor: pointer;
  }
  
  /* 内容区域 */
  .mask-content {
    position: relative;
    background: white;
    border-radius: 12px;
    padding: 30px;
    max-width: 80%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    animation: scaleIn 0.3s ease-out;
  }
  
  /* 关闭按钮 */
  .close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 30px;
    height: 30px;
    border: none;
    background: transparent;
    font-size: 24px;
    cursor: pointer;
    color: #999;
    transition: all 0.2s;
  }
  
  .close-btn:hover {
    color: #333;
    transform: scale(1.1);
  }
  
  /* 动画效果 */
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  
  .fade-enter-from,
  .fade-leave-to {
    opacity: 0;
  }
  
  @keyframes scaleIn {
    0% {
      transform: scale(0.9);
      opacity: 0;
    }
    100% {
      transform: scale(1);
      opacity: 1;
    }
  }
  
  /* 移动端适配 */
  @media (max-width: 768px) {
    .mask-content {
      max-width: 90%;
      padding: 20px;
    }
    
    .close-btn {
      top: 5px;
      right: 5px;
    }
  }
  </style>